package com.ltt.controller;

import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Question;
import com.ltt.dao.pojo.User;
import com.ltt.dao.pojo.UserAnswer;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.UserAnswerParams;
import com.ltt.service.QuestionService;
import com.ltt.service.UserAnswerService;
import com.ltt.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/3 16:17
 */
@Api(tags = "答案模块")
@RestController
@Transactional
@RequestMapping("/answer")
public class UserAnswerController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAnswerService userAnswerService;

    @Autowired
    private QuestionService questionService;

    /**
     * @param userAnswerParams
     * @return com.ltt.dao.vo.Result
     * @description: 先通过用户的答案，与服务器的题目答案比对，然后再进行其他数据的插入
     */
    @ApiOperation("返回正确答案，同时进行答案的判断与积分的增加")
    @PostMapping("/find")
    public Result findAnswer(@RequestBody UserAnswerParams userAnswerParams) {
        // 找到题目和正确答案
        Question question = questionService.getQuestionById(userAnswerParams.getId());

        // 对用户的大体数据进行设置
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAnswer(userAnswerParams.getUserAnswer());
        // 从Thread中获取openid
        String openid = UserThreadLocal.get().getOpenid();

        userAnswer.setOpenid(openid);
        userAnswer.setQuestionId(userAnswerParams.getId());
        userAnswer.setTrueAnswer(question.getAnswer());

        // 判断用户的数据是否正确
        if (userAnswerParams.getUserAnswer().equals(question.getAnswer())) {
            User user = userInfoService.findUserById(openid);
            user.setIntegral(user.getIntegral() + 1);
            user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            // 正确的话需要对积分进行加 1.此处用到了乐观锁
            userInfoService.plusIntegral(user);

            userAnswer.setIsTrue(1);

        } else {
            userAnswer.setIsTrue(0);
        }
        userAnswerService.insertUserAnswer(userAnswer);
        return Result.successWithData(question.getAnswer());
    }

    @ApiOperation("挑战答题，答对+5分")
    @PostMapping("/plusFive")
    public Result plusFive(@RequestBody UserAnswerParams userAnswerParams){
        // 找到题目和正确答案
        Question question = questionService.getQuestionById(userAnswerParams.getId());

        // 对用户的大体数据进行设置
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAnswer(userAnswerParams.getUserAnswer());
        // 从Thread中获取openid
        String openid = UserThreadLocal.get().getOpenid();

        userAnswer.setOpenid(openid);
        userAnswer.setQuestionId(userAnswerParams.getId());
        userAnswer.setTrueAnswer(question.getAnswer());

        if (userAnswerParams.getUserAnswer().equals(question.getAnswer())) {
            User user = userInfoService.findUserById(openid);
            user.setIntegral(user.getIntegral() + 5);
            user.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            // 正确的话需要对积分进行加 1.此处用到了乐观锁
            userInfoService.plusIntegral(user);
        }

        return Result.successWithData(question.getAnswer());
    }
}
